dojo.declare("MRegistrationPg", wm.Page, {

    start: function() {

        var mainPage = wm.Page.getPage("MobileMainPg");
        if (mainPage.vLoginCount && mainPage.vLoginCount === 1) {
            this.restorePhonegapCredentials();
        } else {
            this.populatePhonegapCredentials();
        }
    },
    "preferredDevice": "phone",
    restorePhonegapCredentials: function() {
        var mainPage = wm.Page.getPage("MobileMainPg");

        if (mainPage.phonegapCredentialStorage) {
            var username = mainPage.phonegapCredentialStorage.getValue("name");
            var password = mainPage.phonegapCredentialStorage.getValue("dataValue");
            if (username || password) {
                this.usernameInput.setDataValue(username);
                this.passwordInput.setDataValue(password);
                if (username && password) {
                    this.svLogin.update();
                }
            }
        }
    },
    populatePhonegapCredentials: function() {

        var mainPage = wm.Page.getPage("MobileMainPg");
        if (mainPage.phonegapCredentialStorage) {

            var username = mainPage.phonegapCredentialStorage.getValue("name");
            var password = mainPage.phonegapCredentialStorage.getValue("dataValue");

            if (username || password) {
                this.usernameInput.setDataValue(username);
                this.passwordInput.setDataValue(password);
                if (username && password) {

                }
            }
        }
    },

    svLoginResult: function(inSender, inDeprecated) {
        try {
            var result = inSender.getItem(0);
            var mainPage = wm.Page.getPage("MobileMainPg");
            if (result.data.authenticated === true || result.data.authenticated === 'true') {
                //alert("username=" + this.usernameInput.getDataValue() + ";password=(" + this.passwordInput.getDataValue() + ")");
                mainPage.phonegapCredentialStorage.setData({
                    name: this.usernameInput.getDataValue(),
                    dataValue: this.passwordInput.getDataValue()
                });
                var username = mainPage.phonegapCredentialStorage.getValue("name");
                var password = mainPage.phonegapCredentialStorage.getValue("dataValue");

                mainPage.vLoginCount = 1;
                this.notifyLogin.update();

                mainPage.vUserName = result.data.name;
                mainPage.vUserEmail = result.data.email;
                mainPage.vUserId = result.data.customerId;
                mainPage.vProjectId = result.data.projectId;
                mainPage.vUserRole = result.data.roles;


                // mainPage.pictureHome.setShowing(true);
                this.panelLoginErrorMessage.setShowing(false);
                // mainPage.pictureLogout.setShowing(true);
                //  mainPage.headerPanel.setShowing(false);
                if (result.data.roles === 'CUSTOMER') {
                    mainPage.ncCustomerService.update();
                } else if (result.data.roles === 'VENDOR') {
                    mainPage.ncVendorHomePg.update();
                } else if (result.data.roles === 'ADMIN') {
                    mainPage.ncCustomerService.update();
                }
            } else {
                // this.notifyLoginError.update();
                this.panelLoginErrorMessage.setShowing(true);
                this.loginErrorMsg.setCaption("Invalid username or password.");
                this.passwordInput.clear();
                this.usernameInput.focus();

            }

        } catch (e) {

            this.label1.caption = e;

        }

    },
    buttonRegistrationClick: function(inSender) {
        var isValid = true;
        var errorMsg = "Please enter valid data in required fields";

        if (!this.luBlocks.getDataValue() || (this.luBlocks && this.luBlocks.getDataValue() === "")) {
            isValid = false;
            errorMsg = "Please enter valid Block";
        } else

        if (!this.luDoor.getDataValue() || (this.luDoor && this.luDoor.getDataValue() === "")) {
            isValid = false;
            errorMsg = "Please enter Door";
        } else if (!this.emailEditor1.getDataValue() || (this.emailEditor1 && this.emailEditor1.getDataValue() === "") || this.emailEditor1.getInvalid()) {
            isValid = false;
            errorMsg = "Please enter valid email Id";

        } else if (!this.passwordEditor1.getDataValue() || (this.passwordEditor1 && this.passwordEditor1.getDataValue() === "")) {
            isValid = false;
            errorMsg = "Please enter valid password";
        } else if (!this.nameEditor1.getDataValue() || (this.nameEditor1 && this.nameEditor1.getDataValue() === "")) {
            isValid = false;
            errorMsg = "Please enter Name";
        } else

        if (!this.primaryPhoneEditor1.getDataValue() || (this.primaryPhoneEditor1 && this.primaryPhoneEditor1.getDataValue() === "") || this.primaryPhoneEditor1.getInvalid()) {
            isValid = false;
            errorMsg = "Please enter valid Phone Number";
        } else if (this.pictureRegistrationChkBox.showing) {
            errorMsg = "Please select the copyright";
            isValid = false;

        }
        if (isValid) {
            this.usernameInput.setDataValue(this.emailEditor1.getDataValue());
            this.passwordInput.setDataValue(this.passwordEditor1.getDataValue());
            console.log('calling service for registration');
            this.svRegistration.update();
        } else {
            app.toastPosition = "center center";
            app.toastWarning(errorMsg, 2000);

            //this.ncRequiredFields.text=errorMsg;
            // this.ncRequiredFields.update();
        }
    },
    picture1Click: function(inSender) {
        this.picture2.setShowing(true);
        this.picture1.setShowing(false);
    },
    picture2Click: function(inSender) {
        this.picture2.setShowing(false);
        this.picture1.setShowing(true);

    },
    svRegistrationError: function(inSender, inError) {
        var inMsg = inError.toString();
        var msg = inMsg.substr(inMsg.indexOf('ion:') + 4);
        this.notifyRegistrationError.input.setValue("text", msg);
        this.notifyRegistrationError.update();


    },
    lvValidateActivationCodeResult: function(inSender, inDeprecated) {
        if (inSender.isEmpty()) {

            this.ncInValidActivationCode.update();
            // this.textActivationCode.clear();
            this.textActivationCode.focus();
        } else {

            this.ncValidActivationCode.update();

        }
    },
    textActivationCodeChange: function(inSender, inDisplayValue, inDataValue, inSetByCode) {
        if (this.textActivationCode.isDirty) {
            this.lvValidateActivationCode.update();

        }
    },

    svRegistrationSuccess2: function(inSender, inDeprecated) {

        this.loginButton.click();
    },
    pictureChkBoxClick: function(inSender) {

        this.pictureChkBox.setShowing(false);
        this.pictureChkBoxChecked.setShowing(true);

    },
    pictureChkBoxCheckedClick: function(inSender) {
        this.pictureChkBoxChecked.setShowing(false);
        this.pictureChkBox.setShowing(true);

    },
    loginButtonClick: function(inSender) {

        var invalid = false;
        if (!this.usernameInput.getDataValue || this.usernameInput.getInvalid()) {
            invalid = true;
        }
        if (!this.passwordInput.getDataValue || this.passwordInput.getInvalid()) {

            invalid = true;
        }
        if (invalid) {

            this.ncRequiredFields.update();
        } else {
            if(this.svLogin._requester){
                delete this.svLogin._requester;
              }
             this.svLogin.update();
            // console.log(this.svLogin._requester);
            //alert(this.svLogin._requester);
           
            

        }

        //  loadingDialog.show
    },
    pictureRegistrationClick: function(inSender) {
        if (this.pictureRegistrationChkBox.showing) {
            this.pictureRegistrationChkBox.setShowing(false);
            this.pictureRegistrationChkBoxChecked.setShowing(true);

        } else {
            this.pictureRegistrationChkBox.setShowing(true);
            this.pictureRegistrationChkBoxChecked.setShowing(false);
        }

    },
    pictureRegistrationChkBoxClick: function(inSender) {
        this.pictureRegistrationChkBox.setShowing(false);
        this.pictureRegistrationChkBoxChecked.setShowing(true);

    },
    pictureRegistrationChkBoxCheckedClick: function(inSender) {
        this.pictureRegistrationChkBox.setShowing(true);
        this.pictureRegistrationChkBoxChecked.setShowing(false);

    },
    luBlocksChange: function(inSender, inDisplayValue, inDataValue, inSetByCode) {
        this.luDoor.setOptions(inDataValue.dataValue);
    },
    bResetPasswordClick: function(inSender) {
        if (this.usernameInput.getDataValue() && this.usernameInput.getDataValue() !== "") {
            this.svForgotPassword.update();
        } else {
            app.toastWarning("Please enter email address", 2000);
        }
    },
    registerButtonClick1: function(inSender) {

    },
    labelCoyRight7ReadOnlyNodeFormat: function(inSender, inValue) {
        return "	<a href='tel:080-25723322'>Customer Care 080-25723322</a>";
    },
    _end: 0
});